const TIME_ONE = 60000;
const TIME_TWO = 1000;
const TIME_THREE = 10;

export class TimeUtil {
  public static secondsToMinutesSeconds(seconds: number): string {
    // 计算分钟数（向下取整）
    const minutes = Math.floor(seconds / 60);
    // 计算剩余的秒数
    const remainingSeconds = seconds % 60;

    // 拼接结果字符串
    let result = '';
    if (minutes > 0) {
      result += `${minutes}分`;
    }
    result += `${remainingSeconds}秒`;

    return result;
  }

  // 将毫秒数值转换为分钟:秒数的时间格式字符串
  public static timeConvert(time: number): string {
    let min: number = Math.floor(time / TIME_ONE);
    let second: string = ((time % TIME_ONE) / TIME_TWO).toFixed(0);
    return `${min}:${(+second < TIME_THREE ? '0' : '') + second}`;

  }


  // 时间戳转日常时间格式 (兼容秒/毫秒两种单位)
  public static formatTimestamp(timestamp: number, showSeconds = false): string {
    // 自动识别时间戳单位（13位为毫秒，10位为秒）
    const date = new Date(timestamp > 9999999999 ? timestamp : timestamp * 1000);

    // 补零函数
    const padZero = (num: number) => num.toString().padStart(2, '0');

    // 分解时间元素
    const year = date.getFullYear();
    const month = padZero(date.getMonth() + 1);
    const day = padZero(date.getDate());
    const hours = padZero(date.getHours());
    const minutes = padZero(date.getMinutes());
    const seconds = padZero(date.getSeconds());

    // 构建格式
    const baseFormat = `${year}年${month}月${day}日 ${hours}:${minutes}`;
    return showSeconds ? `${baseFormat}:${seconds}` : baseFormat;
  }

  // 时间戳转日常时间格式 (兼容秒/毫秒两种单位)
  public static formatTimeToMonthDay(timestamp: number, showSeconds = false): string {
    // 自动识别时间戳单位（13位为毫秒，10位为秒）
    const date = new Date(timestamp > 9999999999 ? timestamp : timestamp * 1000);

    // 补零函数
    const padZero = (num: number) => num.toString().padStart(2, '0');

    // 分解时间元素
    const year = date.getFullYear();
    const month = padZero(date.getMonth() + 1);
    const day = padZero(date.getDate());
    const hours = padZero(date.getHours());
    const minutes = padZero(date.getMinutes());
    const seconds = padZero(date.getSeconds());

    // 构建格式
    return `${month}月${day}日 ${hours}:${minutes}`;
  }

  // 新增时间格式化方法（保留1位小数）
  public static formatTime(seconds: number): string {
    const totalSeconds = Math.floor(seconds);
    const hours = Math.floor(totalSeconds / 3600);
    const minutes = Math.floor((totalSeconds % 3600) / 60);
    const secs = totalSeconds % 60;
    const decimal = Math.round((seconds % 1) * 10); // 获取十分位

    return `${hours.toString().padStart(2, '0')}:` +
      `${minutes.toString().padStart(2, '0')}:` +
      `${secs.toString().padStart(2, '0')}.${decimal}`;
  }

  // 在类中添加新的时间格式化方法
  public static formatDisplayTime(seconds: number): string {
    const totalSeconds = Math.floor(seconds);
    const hours = Math.floor(totalSeconds / 3600);
    const minutes = Math.floor((totalSeconds % 3600) / 60);
    const secs = totalSeconds % 60;

    // 根据是否超过1小时决定格式
    if (hours > 0) {
      return `${hours}:${minutes.toString().padStart(2, '0')}:${secs.toString().padStart(2, '0')}`;
    }
    return `${minutes}:${secs.toString().padStart(2, '0')}`;
  }
}

